/*
 * jQuery Validate Fields
 * By Sylvain MATHIEU (www.sylvain-mathieu.com) for Zenexity (zenexity.fr)
 * MIT License (http://www.opensource.org/licenses/mit-license.php)
 */


(function($) {

    $.validate = {
        reSyntax: /^\s*([a-zA-Z_][a-zA-Z_0-9]*)\(([0-9]*|'[^']*')\)/,
        reEmail:  /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9_.-]+\.[a-zA-Z]+$/,

        map: {},

        toMap: function(str) {
            var map = {}, m, val;
            while ((m = $.validate.reSyntax.exec(str))!=null) {
                val = m[2];
                if (val[0]=="'") val = val.substring(1,val.length-1);
                map[m[1]] = val;
                str = str.substring(m[0].length);
            }
            return map;
        },
        fn: function(val,func,param) {
            return $.validate.rules[func](val,param);
        },
        error: function(that,fnName) {
            that.addClass("valko").removeClass("valok");
            var msg;
            if(that.attr("specificmsg")) {
                msg = that.attr("specificmsg");
            }
            else {
                msg = $.validate.lang(fnName);
            }
            $("[rel="+that.attr("id")+"]").text(msg).show();
        },
        success: function(that) {
            that.addClass("valok").removeClass("valko");
            $("[rel="+that.attr("id")+"]").text("").hide();
        },
        lang: function(key) {
            // todo: Possibilité du %s
            return $.validate.lang_messages[key]
        },

        lang_messages: {
            notempty: "This field must not be empty.",
            min: "There is not enough characters.",
            max: "There is too much characters.",
            email: "Email address is not valid.",
            number: "The value must be a number.",
            minnum: "The number is too small.",
            maxnum: "The number is too big.",
            not: "Incorrect value.",
            checked: "The checkbox must be checked.",
            regex: "Incorrect value."
        },

        rules: {
            notempty: function(val) {
                return val.length>0;
            },
            min: function(val,num) {
                return val.length>=num;
            },
            max: function(val,num) {
                return val.length<=num;
            },
            email: function(val) {
                return val.match($.validate.reEmail);
            },
            number: function(val,num) {
                return !isNaN(val);
            },
            minnum: function(val,num) {
                return !isNaN(val)&&parseInt(val)>=num;
            },
            maxnum: function(val,num) {
                return !isNaN(val)&&parseInt(val)<=num;
            },
            not: function(val,str) {
                return val!=str;
            },
            checked: function(val) {
                return val;
            },
            regex: function(val,regex) {
                return val.match(regex);
            }
        },
        validation: function(that) {
            var val = that.attr("type")=="checkbox"?that.attr("checked"):that.val();
            var key;
            var flag = true;
            var map = $.validate.map[that.attr("id")];
            for (key in map) {
                flag = $.validate.fn(val,key,map[key]);
                if (!flag) break;
            }
            if (!flag) {
                $.validate.error(that, key);
                return false;
            }
            else {
                $.validate.success(that);
                return true;
            }
        }
    };

    $.fn.validate = function() {
        return this.each(function() {
            $("[validate]",this).each(function() {
                $.validate.map[$(this).attr("id")] = $.validate.toMap($(this).attr("validate"));
                $(this).blur(function() {
                    $.validate.validation($(this));
                });
            });
            $(this).submit(function() {
                var isValid = true;
                $("[validate]",this).each(function() {
                    if(!$.validate.validation($(this))) isValid = false;
                });
                return isValid;
            });
        });
    };

})(jQuery);
